Skip to content

Expose host/vm powerered state metrics - #42

Open
Zophar78 wants to merge 3 commits into
prezhdarov:masterfrom
Zophar78:feature/power_state_metrics
Open

Expose host/vm powerered state metrics#42
Zophar78 wants to merge 3 commits into
prezhdarov:masterfrom
Zophar78:feature/power_state_metrics

Conversation

@Zophar78

Copy link
Copy Markdown
Contributor

This PR adds explicit state metrics for VMware Hosts and VMs so powered-off entities are still visible in Prometheus.

What’s included:

  • Added host state metrics:
    • vmware_host_powered_on
    • vmware_host_connected
  • vmware_host_maintenance_mode
  • Added VM state metric:
  • vmware_vm_powered_on

Solve #30

@prezhdarov

Copy link
Copy Markdown
Owner

I wondered this or add a label to vm/host_info metrics - my idea behind these were to host all non-metric information about particular vSphere object... but may be cumbersome to use in dashboards if only a label...

@Zophar78

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback.

I considered adding this as labels on vm_info/host_info, but I chose explicit 0/1 metrics because they are easier to use in PromQL, dashboards and alerts.

For example:

  • vmware_vm_powered_on == 0
  • count by (host) (vmware_vm_powered_on == 0)
  • vmware_host_connected == 0

With only labels on *_info metrics, users would need to filter on label values, which is useful for metadata but less convenient for alerting and numeric aggregation.

That said, I agree that power_state/connectivity are also descriptive information. So i let this PR on draft some time (and will rebase it) to see if there are any more feedback. I can still:

  1. keep the explicit metrics only,
  2. add the state as labels on vm_info/host_info as well,
  3. or switch to the *_info label approach only.

@nkvrh

nkvrh commented Jun 29, 2026

Copy link
Copy Markdown

Great PR @Zophar78. Thank you!
I favor using explicit metrics to enable simpler and more cost-effective filtering for both graphing and alerting. While adding this data as a label might not be a major problem in this specific case, I believe using a new metric is a better approach. As stated in the official documentation: 'The change of any label's value, including adding or removing labels, will create a new time series.' (https://prometheus.io/docs/concepts/data_model/)

@Zophar78
Zophar78 force-pushed the feature/power_state_metrics branch from ffe788d to 1c76efc Compare June 29, 2026 13:56
@Zophar78
Zophar78 marked this pull request as ready for review June 29, 2026 14:09
@Zophar78

Copy link
Copy Markdown
Contributor Author

Ok PR is now ready for review/merge.

Comment thread vmware/collectors/vm.go Outdated
vmLabels := map[string]string{
"vmmo": vm.Self.Value,
"vm": vm.Summary.Config.Name,
"hostmo": vm.Runtime.Host.Value,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Runtime.Host can be nil per Broadcom's vSphere API docs when the VM is not running and is not assigned to run on a particular host.

https://developer.broadcom.com/xapis/virtual-infrastructure-json-api/latest/data-structures/VirtualMachineRuntimeInfo/

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look on this one

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the catch, you were right. I’ve pushed a fix to handle Runtime.Host being nil for powered-off/unassigned VMs, based on the VirtualMachineRuntimeInfo docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants